package ru.braim.weather_app.controllers;

import jakarta.annotation.Nullable;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import ru.braim.weather_app.dto.region.RegionRequestDTO;
import ru.braim.weather_app.dto.region.RegionResponseDTO;
import ru.braim.weather_app.models.Region;
import ru.braim.weather_app.responses.region.RegionResponse;
import ru.braim.weather_app.services.CookieService;
import ru.braim.weather_app.services.RegionService;

@RestController
@RequestMapping("/region")
public class RegionController {

    private final RegionService regionService;
    private final CookieService cookieService;

    public RegionController(RegionService regionService, CookieService cookieService) {
        this.regionService = regionService;
        this.cookieService = cookieService;
    }


    @PostMapping("")
    public ResponseEntity<RegionResponseDTO> createRegion(@RequestBody RegionRequestDTO regionRequestDTO,
                                                          @CookieValue("id") @Nullable String cookie) {

        if (!cookieService.checkAuthorizeCookie(cookie)) {
            return new ResponseEntity<>(null, HttpStatus.UNAUTHORIZED);
        }

        RegionResponse regionResponse = regionService.createRegion(regionRequestDTO, cookie);

        return new ResponseEntity<>(regionResponse.getRegionResponseDTO(), regionResponse.getHttpStatus());

    }

    @GetMapping("/{regionId}")
    public ResponseEntity<RegionResponseDTO> getRegionById(@PathVariable String regionId,
                                                          @CookieValue("id") @Nullable String cookie) {
        try {
            if (!cookieService.checkAuthorizeCookie(cookie)) {
                return new ResponseEntity<>(null, HttpStatus.UNAUTHORIZED);
            }

            RegionResponse regionResponse = regionService.getRegionById(Long.parseLong(regionId));

            return new ResponseEntity<>(regionResponse.getRegionResponseDTO(), regionResponse.getHttpStatus());
        } catch (NumberFormatException e) {
            return new ResponseEntity<>(null, HttpStatus.BAD_REQUEST);
        }

    }

    @PutMapping("/{regionId}")
    public ResponseEntity<RegionResponseDTO> updateRegionById(@RequestBody RegionRequestDTO regionRequestDTO,
                                                              @PathVariable String regionId,
                                                                @CookieValue("id") @Nullable String cookie) {
        try {

            if (!cookieService.checkAuthorizeCookie(cookie)) {
                return new ResponseEntity<>(null, HttpStatus.UNAUTHORIZED);
            }

            RegionResponse regionResponse = regionService.updateRegionById(Long.parseLong(regionId), regionRequestDTO);

            return new ResponseEntity<>(regionResponse.getRegionResponseDTO(), regionResponse.getHttpStatus());
        } catch (NumberFormatException e) {
            return new ResponseEntity<>(null, HttpStatus.BAD_REQUEST);
        }

    }

    @DeleteMapping("/{regionId}")
    public ResponseEntity<RegionResponseDTO> deleteRegionById(@PathVariable String regionId,
                                                              @CookieValue("id") @Nullable String cookie) {
        try {

            if (!cookieService.checkAuthorizeCookie(cookie)) {
                return new ResponseEntity<>(null, HttpStatus.UNAUTHORIZED);
            }

            RegionResponse regionResponse = regionService.deleteRegionById(Long.parseLong(regionId));

            return new ResponseEntity<>(regionResponse.getRegionResponseDTO(), regionResponse.getHttpStatus());
        } catch (NumberFormatException e) {
            return new ResponseEntity<>(null, HttpStatus.BAD_REQUEST);
        }

    }

}
